package appapi.mqtt;

import appapi.entity.mqtt.Topic;
import appapi.utils.RedisUtils;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.util.JSONUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2024/01/15 14:52
 */
@Slf4j
@Component
public class InitCallback implements MqttCallback {


    @Resource
    private RedisUtils redisUtils;

    /**
     * MQTT 断开连接会执行此方法
     */
    @Override
    public void connectionLost(Throwable cause) {
        log.error(cause.getMessage(), cause);
    }

    /**
     * publish发布成功后会执行到这里
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
    }

    /**
     * subscribe订阅后得到的消息会执行到这里
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) {
        String data = "";
        Topic res = new Topic(topic);
        try {
            data = new String(message.getPayload());
            if (!"".equals(data)) {
                String upload = res.getDataType().toLowerCase();
                switch (upload) {
                    //设备实时数据
                    case "data":
                        //写实时数据至iotDB
                        //1.取最后记录时间
                        Object value = redisUtils.get("real_" + res.getEquiNo());
                        Long lasttime = 0L;
                        if (value != null) {
                            lasttime = Long.parseLong(value.toString());
                        }
                        //2.json格式化数据
                        JsonNode obj = new ObjectMapper().readTree(data);
                        Long timestamp = Long.parseLong(obj.get("timestamp").toString());
                        //3.是否在5秒内
                        //if (timestamp - lasttime > 5000)
                    {
                        //4.记录存es库
                        //saveRealDataToDB(res.getEquiNo(),data);
//                        saveRealDataToDBBulk(res.getEquiNo(), data);
                        redisUtils.set("real_" + res.getEquiNo(), timestamp);
                        redisUtils.set("real_data_" + res.getEquiNo(), data);
                    }
                    break;
                    case "alarm":
                        //1.取最后记录时间
                        Object value1 = redisUtils.get("real_" + res.getEquiNo());
                        Long lasttime1 = 0L;
                        if (value1 != null) {
                            lasttime1 = Long.parseLong(value1.toString());
                        }
                        //2.json格式化数据
                        JsonNode obj1 = new ObjectMapper().readTree(data);
                        Long timestamp1 = Long.parseLong(obj1.get("timestamp").toString());
                        //3.是否在5秒内
                        //if (timestamp - lasttime > 5000)
                    {
                        //4.记录存es库
                        //saveRealDataToDB(res.getEquiNo(),data);
//                        saveRealDataToDBBulk(res.getEquiNo(), data);
                        redisUtils.set("real_" + res.getEquiNo(), timestamp1);
                        redisUtils.set("real_alarm_" + res.getEquiNo(), data);
                    }
                        break;
                    case "workset":
                       redisUtils.set("dfdsf",res.getEquiNo());
                        break;
                    default:
                        break;
                }
            }
        } catch (Exception ex) {
            log.info("Exception : {}", topic + message + ex.getMessage());
        }
    }


}
